/* 11章.指针 */
#include <stdio.h>

#define N 10

void decompose(double x, long *int_part, double *fract_part);
void max_min(int a[], int n, int *max, int *min);
int *max(int *a, int *b);
void avg_sum(double a[], int n, double *avg, double *sum);
void swap(int *p, int *q);
void find_two_largest(int a[], int n, int *largest, int *second_largest);
int *find_largest(int a[], int n);

int main(void) {
    // q和q是指向整型对象的指针
    int i, j, *p, *q;

    double *q1;
    char *r;

    int *p1 = &i;

    printf("%d\n", *p1);
    i=1;
    printf("%d\n", *p1);
    printf("%d\n", i);
    *p1=2;
    printf("%d\n", *p1);
    printf("%d\n", i);

    double d;
    decompose(3.1415926, &i, &d);
    printf("i=%d\n", *p1); // print 3
    printf("d=%f\n", d); // print 0.141593

    int a[] = {9,8,7,6,5,4,3,2,1, 10};
    int big, small;
    max_min(a, N, &big, &small);
    printf("Largest: %d\n", big);
    printf("Smallest: %d\n", small);

    p = max(&i, &j);
    printf("max=%d\n", *p);

    int i2 = 3, j2 = 5, j3 = 7, *p2 = &j2, *p3 = &j3;
//    p2 = &j2;
//    *p2 = *p3;

    double avg = 0, sum = 0;
    avg_sum((double[]){1,7,3,4,5}, 5, &avg, &sum);
    printf("sum = %1f, avg=%1f\n", sum, avg);

    int i4 = 1, j4 = 5;
    swap(&i4, &j4);
    printf("i4=%d, j4=%d\n", i4, j4);

    int largest, second_largest;
    find_two_largest((int []){0, 1,2,3,4,7,8}, 7, &largest, &second_largest);
    printf("largest=%d, second_largest=%d\n", largest, second_largest);

    int *max_index = find_largest((int[]) {5, 4, 3, 2, 1}, 5);
    printf("max is %d\n", *max_index);

    int a1[10] = {1,2,3,4,5,6,7,8,9,10}, *p10;
    p10 = a1;
    printf("p10=%d\n", *p10);
    return 0;
}

void decompose(double x, long *int_part, double *fract_part) {
    *int_part = (long) x;
    *fract_part = x - *int_part;
}

void max_min(int a[], int n, int *max, int *min) {
    *max = *min = a[0];

    for(int i = 1; i < n; i++) {
        if(a[i] > *max) {
            *max = a[i];
        }

        if(a[i] < *min) {
            *min = a[i];
        }
    }
}

void f(const int *p) {
//    *p = 0; 这行代码会报错
}

int *max(int *a, int *b) {
    if(*a > *b)
        return a;
    else
        return b;
}

void avg_sum(double a[], int n, double *avg, double *sum) {
    int i;
    *sum = 0.0;
    for (i = 0; i < n; ++i) {
        *sum += a[i];
    }
    *avg = *sum / n;
}

void swap(int *p, int *q) {
    int i = *p;
    *p = *q;
    *q = i;
}

void find_two_largest(int a[], int n, int *largest, int *second_largest) {
    *largest = a[0];
    *second_largest = a[0];

    for (int i = 1; i < n; ++i) {
        if(a[i] > *largest) {
            *second_largest = *largest;
            *largest = a[i];
        }

        if(a[i] > *second_largest && a[i] < *largest) {
            *second_largest = a[i];
        }
    }
}

int *find_largest(int a[], int n) {
    int max_index = 0;
    for (int i = 1; i < n; ++i) {
        if(a[i] > a[max_index]) {
            max_index = i;
        }
    }
    return &a[max_index];
}


int *find_largest_point(const int a[], int n) {
    int *p = a;
    int *max_p = a;
    for (; p < a + n; ++p) {
        if(*p > *max_p) {
            max_p = p;
        }
    }
    return max_p;
}
